package io.cici.cc.mybatis.lite.mapping;

import io.cici.cc.mybatis.lite.session.Configuration;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@Setter
@Getter
public class ResultMappings {
    private Configuration configuration;

    private String id;
    private Class<?> type;


    private List<ResultMapping> resultMappingList;
    private Set<String> mappedColumns;
    private Set<String> mappedProperties;


    private ResultMappings() {
    }


    @Slf4j
    @Setter
    @Getter
    public static class Builder {


        private final ResultMappings resultMappings = new ResultMappings();


        public Builder(Configuration configuration, String id, Class<?> type, List<ResultMapping> resultMappingList) {
            resultMappings.configuration = configuration;
            resultMappings.id = id;
            resultMappings.type = type;
            resultMappings.resultMappingList = resultMappingList;

        }

        public Class<?> type() {
            return resultMappings.type;
        }

        public ResultMappings build() {
            if (resultMappings.id == null) {
                throw new IllegalArgumentException("ResultMaps must have an id");
            }
            resultMappings.mappedColumns = new HashSet<>();
            resultMappings.mappedProperties = new HashSet<>();


            resultMappings.resultMappingList = new ArrayList<>();

            for (ResultMapping resultMapping : resultMappings.resultMappingList) {

                final String column = resultMapping.getColumn();
                resultMappings.mappedColumns.add(column.toUpperCase());
                final String property = resultMapping.getProperty();
                resultMappings.mappedProperties.add(property);
            }

            resultMappings.resultMappingList = Collections.unmodifiableList(resultMappings.resultMappingList);
            resultMappings.mappedColumns = Collections.unmodifiableSet(resultMappings.mappedColumns);
            return resultMappings;
        }


    }

}
